home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / GETKEY.ASM < prev    next >
Assembly Source File  |  1993-05-05  |  5KB  |  142 lines

  1. ;****************************************
  2. ; GETKEY -- By Eric Tauck
  3. ;
  4. ; This program displays a prompt and then
  5. ; waits for and retrieves a keystroke from
  6. ; the keyboard.  The ASCII code of the
  7. ; key is compared to a list of characters
  8. ; on the command line and an error code
  9. ; index from right to left (starting at
  10. ; one) is returned.  This is useful for
  11. ; accepting input within batch files.
  12. ; Example batch file:
  13. ;
  14. ;  :loop
  15. ;  ECHO OFF
  16. ;  CLS
  17. ;  ECHO Application Menu
  18. ;  ECHO   A. Application One
  19. ;  ECHO   B. Application Two
  20. ;  ECHO   C. Finished
  21. ;  getkey "Enter choice: " abc
  22. ;  IF ERRORLEVEL 3 GOTO app1
  23. ;  IF ERRORLEVEL 2 GOTO app2
  24. ;  IF ERRORLEVEL 1 GOTO finished
  25. ;  GOTO loop
  26. ;  :app1
  27. ;  REM run application one
  28. ;  GOTO loop
  29. ;  :app2
  30. ;  REM run application two
  31. ;  GOTO loop
  32. ;  :finished
  33. ;  REM exit batch file
  34.  
  35.         mov     al, [0080H]     ;load bytes in command line
  36.         mov     si, 0081H       ;load address of command line data
  37.  
  38.         or      al, al          ;check if any characters
  39.         jz      b2              ;goto help message if not
  40.  
  41. ;--- skip starting delimiters
  42.  
  43. b1      cmp     BYTE [si], 32   ;check if space or control character
  44.         ja      b3              ;exit loop if so
  45.         inc     si              ;next character
  46.         dec     al              ;decrement character count
  47.         jnz     b1              ;loop back if more characters
  48.  
  49. b2      mov     ah, 9           ;print string function
  50.         mov     dx, OFFSET help ;address of help message
  51.         int     21H             ;execute
  52.         sub     bl, bl          ;return code
  53.         jmps    b9              ;goto exit
  54.  
  55. ;--- found start of characters, display message
  56.  
  57. b3      cmp     BYTE [si], '"'  ;check if double quote
  58.         jne     b2              ;exit if so, goto help message
  59.         mov     bl, al          ;save character count in BL
  60.  
  61. b4      inc     si
  62.         dec     bl
  63.         jz      b2
  64.         mov     dl, [si]        ;load character
  65.         cmp     dl, '"'         ;check if end of prompt
  66.         je      b5
  67.         mov     ah, 2           ;display function
  68.         int     21H             ;execute
  69.         jmp     b4              ;loop back
  70.  
  71. ;--- skip separating delimiters
  72.  
  73. b5      inc     si              ;next character
  74.         dec     bl              ;decrement character count
  75.         jz      b2              ;goto help message if out of characters
  76.         cmp     BYTE [si], 32   ;check if space or control character
  77.         jbe     b5              ;continue looping if so
  78.         
  79. ;--- get a keystroke
  80.  
  81.         mov     ah, 8           ;get input function
  82.         int     21H             ;execute
  83.         or      al, al          ;check if extended
  84.         jnz     b6              ;jump if not
  85.         mov     ah, 8           ;get second code
  86.         int     21H             ;execute
  87.         sub     bl, bl          ;return code
  88.         jmps    b9              ;goto exit, can't handle special keys
  89.  
  90. ;--- display key
  91.  
  92. b6      mov     dl, al          ;character to display
  93.         mov     ah, 2           ;display function
  94.         int     21H             ;execute
  95.  
  96. ;--- convert to uppercase
  97.  
  98.         cmp     dl, 'a'         ;check if too low
  99.         jb      b7
  100.         cmp     dl, 'z'         ;check if too high
  101.         ja      b7
  102.         sub     dl, 'a'-'A'     ;convert to uppercase
  103.  
  104. ;--- search for character
  105.  
  106. b7      mov     al, [si]        ;load character
  107.         cmp     al, 'a'         ;check if too low
  108.         jb      b8              ;jump if so
  109.         cmp     al, 'z'         ;check if too high
  110.         ja      b8              ;jump if so
  111.         sub     al, 'a'-'A'     ;convert to uppercase
  112.  
  113. b8      cmp     al, dl          ;check if match
  114.         je      b9              ;jump if so
  115.         inc     si              ;next character
  116.         dec     bl              ;decrement return code and bytes left
  117.         jnz     b7              ;loop back if more
  118.  
  119. ;--- finished, error code in AL
  120.  
  121. b9      mov     al, bl          ;return code
  122.         mov     ah, 4CH         ;exit function
  123.         int     21H             ;execute
  124.  
  125. ;--- help message
  126.  
  127. help    DB      13,10
  128.         DB      '  GETKEY -- By Eric Tauck',13,10
  129.         DB      13,10
  130.         DB      '  Usage: GETKEY "xxxxx" xxxxx',13,10
  131.         DB      13,10
  132.         DB      '    "xxxxx" is a text prompt (use "" for no prompt)',13,10
  133.         DB      '     xxxxx is a sequence of possible input characters',13,10
  134.         DB      13,10
  135.         DB      '  Displays a prompt and then fetches and translates',13,10
  136.         DB      '  a keystroke to a DOS level error code based on the',13,10
  137.         DB      '  sequence of characters.  The error code may be used',13,10
  138.         DB      '  to branch in a batch file.  The last character re-',13,10
  139.         DB      '  turns error code one, the second to last returns',13,10
  140.         DB      '  error code two, etc.',13,10
  141.         DB      '$'
  142.